//
//  ZhuihaoView.m
//  SSC
//
//  Created by 红沙尘 on 17/6/24.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "ZhuihaoView.h"
#import "ZhuihaoHeaderView.h"
#import "ZhuihaoCell.h"

@interface ZhuihaoView ()<UITableViewDelegate,UITableViewDataSource,ZhuihaoHeaderViewDelegate>
{
    NSMutableArray *dataArray;
    
    
    
    
}

@property (nonatomic, assign) NSInteger startIndex;
@property (nonatomic, copy)   NSString  *qiNumber;
@property (nonatomic, copy)   NSArray   *qiArray;


@end

@implementation ZhuihaoView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selected = YES;
    
    dataArray = [[NSMutableArray alloc] init];
    
    mainTableView.rowHeight = 22;
    mainTableView.estimatedRowHeight = 22;
    
    [mainTableView registerNib:[UINib nibWithNibName:@"ZhuihaoCell" bundle:nil]
        forCellReuseIdentifier:@"ZhuihaoCell"];

    mainTableView.tableFooterView = [UIView new];
}

- (void)setOtype:(CaipiaoType)otype {
    _otype = otype;
    
    mainTableView.tableHeaderView = nil;
    
    ZhuihaoHeaderView *headView = [ZhuihaoHeaderView viewWithDelegate:self type:self.otype];
    headView.frame = CGRectMake(0, 0, ScreenWidth, 350);
    headView.backgroundColor = [UIColor whiteColor];
    mainTableView.tableHeaderView = headView;
}

#pragma mark - IBAction

- (IBAction)selectButtonPressed:(UIButton *)button {
    
    self.selected = !self.selected;
    
    if (self.selected) {
        zhongjiangView.image = LoadImage(@"TouzhuQuanxuanB");
    } else {
        zhongjiangView.image = LoadImage(@"TouzhuQuanxuanA");
    }
}

#pragma mark - ZhuihaoHeaderViewDelegate

- (void)shengchengzuihaoWithQi:(NSInteger)qiCount
                      qiNumber:(NSString *)qiNumber
                        beishu:(NSString *)beishu
                         array:(NSArray *)array
                    startIndex:(NSInteger)startIndex {
    
    self.qiCount = qiCount;
    self.qiNumber = qiNumber;
    self.beishu = beishu;
    self.qiArray = array;
    self.startIndex = startIndex;
    
    [dataArray removeAllObjects];
    
    self.money = [NSString stringWithFormat:@"%d",2*[beishu intValue]*[self.zhushu intValue]];
        
    for (NSInteger i = startIndex; i < startIndex + qiCount; i ++) {
        
        if (i == startIndex) {//保存起始期号
            self.startIssue = array[i][@"issue"];
        }
        
        NSDictionary *dict = @{@"qici":array[i][@"issue"],
                               @"beishu":beishu,
                               @"money":self.money};
        [dataArray addObject:dict];
    }
    
    [mainTableView reloadData];

    zuihaoLabel.text = [NSString stringWithFormat:@"共追号%d期 总投注金额%ld元",(int)qiCount,[self.money intValue]*qiCount];
}

#pragma mark - table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (dataArray.count > 0) {
        
        fanganBtn.userInteractionEnabled = YES;
        fanganBtn.backgroundColor = COLOR_FF5253;
        return 0.01;
    }
    
    fanganBtn.userInteractionEnabled = NO;
    fanganBtn.backgroundColor = COLOR_CCCCCC;
    
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (dataArray.count > 0) return nil;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, ScreenWidth - 24, 40)];
    label.layer.borderColor = COLOR_CCCCCC.CGColor;
    label.layer.borderWidth = LineHeight;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"暂未生成追号计划~";
    label.textColor = COLOR_999999;
    label.font = [MTool fontOfSize:14];
    
    [view addSubview:label];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (dataArray.count == 0) return 0;
    
    return dataArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZhuihaoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhuihaoCell"];
    
    if (indexPath.row == 0) {
        
        NSMutableDictionary *dict = @{@"qici":@"追号期次",
                                      @"beishu":@"追号倍数",
                                      @"money":@"追号金额"}.mutableCopy;
        
        [cell loadWithDict:dict atIndex:0];
    } else {
        [cell loadWithDict:dataArray[indexPath.row - 1] atIndex:indexPath.row];
    }
    
    
    return cell;
}



@end
